package loupan

import (
	"hft/Database/admin/loupan_db"
	"hft/Http/Services/loupan"
	"hft/Http/Services/loupan/loupan_model"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type PlatForm struct{}

func (*PlatForm) Add(c *gin.Context) {
	var data *loupan_db.Platform
	c.ShouldBindJSON(&data)
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage(e.InValidToken, c)
	} else {
		err = loupan.PlatFormService.Add(data, claims.BaseClaims)
		if err != nil {
			utils.FailWithMessage(err.Error(), c)
			return
		}
		utils.Ok(c)
	}
}

func (*PlatForm) Edit(c *gin.Context) {
	var data *loupan_db.Platform
	c.ShouldBindJSON(&data)
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage(e.InValidToken, c)
	} else {
		err = loupan.PlatFormService.Edit(data, claims.BaseClaims)
		if err != nil {
			utils.FailWithMessage(err.Error(), c)
			return
		}
		utils.Ok(c)
	}
}

func (*PlatForm) Delete(c *gin.Context) {
	var data *loupan_model.DeletePlat
	c.ShouldBindJSON(&data)
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage(e.InValidToken, c)
	} else {
		err = loupan.PlatFormService.Delete(claims.BaseClaims, *data)
		if err != nil {
			utils.FailWithMessage(err.Error(), c)
			return
		}
		utils.Ok(c)
	}
}

func (*PlatForm) List(c *gin.Context) {
	var louPanID, _ = strconv.Atoi(c.DefaultQuery("lou_pan_id", "0"))
	if louPanID == 0 {
		utils.FailWithMessage(e.LouPanNotExist, c)
		return
	}
	if list, err := loupan.PlatFormService.List(louPanID); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(list, c)
	}

}

var PlatFormController = new(PlatForm)
